package com.lc.afbx.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.Conn;
import com.lc.afbx.conn.PostIDCard;
import com.lc.afbx.conn.PostVehicleCard;
import com.lc.afbx.dialog.PictureDialog;
import com.lc.afbx.dialog.ShowPicDialog;
import com.lc.afbx.util.Base64Encoder;
import com.lc.afbx.util.DateTimePickDialogUtil;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PictureUtil;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilMD5;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/29.
 */
public class BusinessXubaoFillInfoActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.back) private RelativeLayout back; //返回按钮

    @BoundView(R.id.xubao_fill_applicant_et) private EditText xubao_fill_applicant_et;//投保人
    @BoundView(R.id.xubao_fill_idcard_iv) private SimpleDraweeView xubao_fill_idcard_iv;//展示身份证的
    @BoundView(R.id.xubao_name_et) private EditText xubao_name_et; //被保险人姓名
    @BoundView(R.id.xubao_id_number_et) private EditText xubao_id_number_et; //被保险人身份证号
    @BoundView(R.id.xubao_fill_tel_et) private EditText xubao_fill_tel_et; //被保险人手机号
    @BoundView(R.id.xubao_fill_address_et) private EditText xubao_fill_address_et; //被保险人地址
    @BoundView(R.id.xubao_fill_show_driverCard_iv) private SimpleDraweeView xubao_fill_show_driverCard_iv;//行驶证展示的iv
    @BoundView(R.id.xubao_fill_brand_model_et) private EditText xubao_fill_brand_model_et; // 厂牌型号
    @BoundView(R.id.xubao_fill_plate_num_et) private EditText xubao_fill_plate_num_et; // 号牌号码  就是车牌号
    @BoundView(R.id.xubao_fill_vin_et) private EditText xubao_fill_vin_et; // vin  就是车架号
    @BoundView(R.id.xubao_fill_engine_et) private EditText xubao_fill_engine_et; // 发动机编号
    @BoundView(R.id.xubao_fill_useNature_et) private EditText xubao_fill_useNature_et; // 车辆使用性质
    @BoundView(R.id.xubao_fill_carType_et) private EditText xubao_fill_carType_et; // 车辆类型
    @BoundView(R.id.xubao_fill_regtime_tv) private TextView xubao_fill_regtime_tv; // 车辆注册时间
    @BoundView(R.id.xubao_fill_passengers_et) private EditText xubao_fill_passengers_et; // 车辆核定载客人数
    @BoundView(R.id.xubao_fill_weight_et) private EditText xubao_fill_weight_et; // 车辆核定载质量
    @BoundView(R.id.xubao_fill_driving_area_et) private EditText xubao_fill_driving_area_et; // 车辆行驶区域
    @BoundView(R.id.xubao_fill_place_of_production_tv) private TextView xubao_fill_place_of_production_tv; // 车辆产地
    @BoundView(R.id.xubao_fill_show_peopleCar_iv) private SimpleDraweeView xubao_fill_show_peopleCar_iv; // 人车照片
    @BoundView(R.id.xubao_fill_summit_bt) private Button xubao_fill_summit_bt; // 下一页 提交按钮

//    private PictureDialog pictureDialog;
    private Map<Integer, String> picMap = new HashMap<>();

    private List<String> produceLists = new ArrayList<>();
    private String[] products = {"中国","合资","国外"};

    private int i; //区分图片的
    private int natureType; // 0 没选，1营运，2非营运，3填错

    private Bundle bundle;


//    private PostIDCard postIDCard = new PostIDCard(new AsyCallBack<PostIDCard.IdCardInfoEntity>() {
//        @Override
//        public void onSuccess(String toast, int type, PostIDCard.IdCardInfoEntity idCardInfoEntity) throws Exception {
//
////            String code, result, name, gender, people, byear, bmonth, bday, address, idnumber, authority, validdate, age;
//
//            if (!TextUtils.isEmpty(idCardInfoEntity.name) && !TextUtils.isEmpty(idCardInfoEntity.idnumber) && !TextUtils.isEmpty(idCardInfoEntity.address)) {
//
//                xubao_name_et.setText(idCardInfoEntity.name);
//                xubao_id_number_et.setText(idCardInfoEntity.idnumber);
//                xubao_fill_address_et.setText(idCardInfoEntity.address);
//
//                xubao_idCard_submit_bt.setText("已选择");
//                xubao_fill_idcard_iv.setVisibility(View.VISIBLE);
//                xubao_fill_idcard_iv.setImageBitmap(PictureUtil.getSmallBitmap(picMap.get(1)));
//
//            } else {
//
//                MyToast.showToast("身份证识别失败", context);
//
//                xubao_fill_idcard_iv.setVisibility(View.GONE);
//                xubao_idCard_submit_bt.setText("上传");
//                picMap.put(1, null);
//            }
//
//        }
//
//        @Override
//        public void onFail(String toast, int type) throws Exception {
//
//            MyToast.showToast("身份证识别失败", context);
//
//            xubao_fill_idcard_iv.setVisibility(View.GONE);
//            xubao_idCard_submit_bt.setText("上传");
//            picMap.put(1, null);
//
//        }
//    });
//
//    private PostVehicleCard postVehicleCard = new PostVehicleCard(new AsyCallBack<PostVehicleCard.VehicleCardEntity>() {
//
//        @Override
//        public void onSuccess(String toast, int type, PostVehicleCard.VehicleCardEntity vehicleCardEntity) throws Exception {
//
//
//            if (!TextUtils.isEmpty(vehicleCardEntity.plateno) && !TextUtils.isEmpty(vehicleCardEntity.vehicletype) && !TextUtils.isEmpty(vehicleCardEntity.usecharacter) && !TextUtils.isEmpty(vehicleCardEntity.engineno) && !TextUtils.isEmpty(vehicleCardEntity.vin) && !TextUtils.isEmpty(vehicleCardEntity.model) && !TextUtils.isEmpty(vehicleCardEntity.registerdate) && !TextUtils.isEmpty(vehicleCardEntity.usecharacter)) {
//
//                xubao_fill_brand_model_et.setText(vehicleCardEntity.model);
//                xubao_fill_plate_num_et.setText(vehicleCardEntity.plateno);
//                xubao_fill_vin_et.setText(vehicleCardEntity.vin);
//                xubao_fill_engine_et.setText(vehicleCardEntity.engineno);
//                xubao_fill_useNature_et.setText(vehicleCardEntity.usecharacter);
//                xubao_fill_carType_et.setText(vehicleCardEntity.vehicletype);//车辆类型 这个要判断 传数字还是汉字
//                xubao_fill_regtime_tv.setText(vehicleCardEntity.registerdate);
//
//                xubao_fill_carType_et.setTag("1");//这瞎写的  以后要判断
//
//                xubao_fill_show_driverCard_iv.setVisibility(View.VISIBLE);
//                xubao_fill_show_driverCard_iv.setImageBitmap(PictureUtil.getSmallBitmap(picMap.get(2)));
//                xubao_fill_driverCard_submit_bt.setText("已选择");
//
//            } else {
//
//                MyToast.showToast("行驶证识别失败", context);
//
//                xubao_fill_show_driverCard_iv.setVisibility(View.GONE);
//                xubao_fill_driverCard_submit_bt.setText("上传");
//                picMap.put(2, null);
//
//            }
//
//
//        }
//
//        @Override
//        public void onFail(String toast, int type) throws Exception {
//
//            MyToast.showToast("行驶证识别失败", context);
//
//            xubao_fill_show_driverCard_iv.setVisibility(View.GONE);
//            xubao_fill_driverCard_submit_bt.setText("上传");
//            picMap.put(2, null);
//
//        }
//
//    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouye_shangyexian_xubao_fill_info);


        Collections.addAll(produceLists, products);

//        pictureDialog = new PictureDialog(context) {
//            @Override
//            protected void onCamera() {
//
//                startCamera(null);
//
//            }
//
//            @Override
//            protected void onAlbum() {
//
//                startAlbum(null);
//
//            }
//        };

        xubao_fill_useNature_et.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (s.toString().equals("营运")) {

                    findViewById(R.id.weight_shangyexian_rl).setVisibility(View.VISIBLE);
                    natureType = 1;


                } else if (s.toString().equals("非营运")) {

                    findViewById(R.id.weight_shangyexian_rl).setVisibility(View.GONE);
                    natureType = 2;

                } else {

                    MyToast.showToast("请填写 营运或非营运", context);
                    natureType = 3;

                }

            }
        });

        setListener();

        initViews();

    }

    private void initViews() {

        bundle = getIntent().getExtras();

        xubao_fill_applicant_et.setText(bundle.getString("applicant"));//投保人
        xubao_name_et.setText(bundle.getString("name")); //被保险人姓名
        xubao_id_number_et.setText(bundle.getString("id_number")); //被保险人身份证号
        xubao_fill_tel_et.setText(bundle.getString("tel")); //被保险人手机号
        xubao_fill_address_et.setText(bundle.getString("address")); //被保险人地址
        xubao_fill_brand_model_et.setText(bundle.getString("brand_model")); // 厂牌型号
        xubao_fill_plate_num_et.setText(bundle.getString("plate_num")); // 号牌号码  就是车牌号
        xubao_fill_vin_et.setText(bundle.getString("vin_code")); // vin  就是车架号
        xubao_fill_engine_et.setText(bundle.getString("engine_num")); // 发动机编号

        if (bundle.getString("use_nature").equals("1")) {

            xubao_fill_useNature_et.setText("营运"); // 车辆使用性质

        } else {

            xubao_fill_useNature_et.setText("非营运"); // 车辆使用性质

        }
        xubao_fill_carType_et.setText(bundle.getString("vehicle_type")); // 车辆类型
        xubao_fill_regtime_tv.setText(bundle.getString("regtime")); // 车辆注册时间
        xubao_fill_passengers_et.setText(bundle.getString("passengers")); // 车辆核定载客人数
        xubao_fill_weight_et.setText(bundle.getString("weight")); // 车辆核定载质量
        xubao_fill_driving_area_et.setText(bundle.getString("driving_area")); // 车辆行驶区域
        xubao_fill_place_of_production_tv.setText(bundle.getString("vehicle_origin")); // 车辆产地

        picMap.put(1, bundle.getString("id_url_old"));
        picMap.put(2, bundle.getString("drive_url_old"));
        picMap.put(3, bundle.getString("people_car_url_old"));

        xubao_fill_idcard_iv.setImageURI(Uri.parse(Conn.PIC + bundle.getString("id_url_old")));
        xubao_fill_show_driverCard_iv.setImageURI(Uri.parse(Conn.PIC + bundle.getString("drive_url_old")));
        xubao_fill_show_peopleCar_iv.setImageURI(Uri.parse(Conn.PIC + bundle.getString("people_car_url_old")));

    }

    private void setListener() {

        back.setOnClickListener(this);
        xubao_fill_regtime_tv.setOnClickListener(this);
        xubao_fill_summit_bt.setOnClickListener(this);
        xubao_fill_place_of_production_tv.setOnClickListener(this);
        xubao_fill_idcard_iv.setOnClickListener(this);
        xubao_fill_show_driverCard_iv.setOnClickListener(this);
        xubao_fill_show_peopleCar_iv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                
                finish();

                break;

            case R.id.xubao_fill_regtime_tv:

                DateTimePickDialogUtil dialogUtil = new DateTimePickDialogUtil(this, null);
                dialogUtil.dateTimePicKDialog(xubao_fill_regtime_tv);

                break;

            case R.id.xubao_fill_place_of_production_tv:

                showPop(xubao_fill_place_of_production_tv);

                break;

            case R.id.xubao_fill_idcard_iv:

                new ShowPicDialog(Conn.PIC + picMap.get(1), context).show();

                break;

            case R.id.xubao_fill_show_driverCard_iv:

                new ShowPicDialog(Conn.PIC + picMap.get(2), context).show();

                break;

            case R.id.xubao_fill_show_peopleCar_iv:

                new ShowPicDialog(Conn.PIC + picMap.get(3), context).show();

                break;

            case R.id.xubao_fill_summit_bt:

                String xubao_fill_applicant_str = xubao_fill_applicant_et.getText().toString();//投保人
                String xubao_name_str = xubao_name_et.getText().toString();//被保险人姓名
                String xubao_id_number_str = xubao_id_number_et.getText().toString();//被保险人身份证号
                String xubao_fill_tel_str = xubao_fill_tel_et.getText().toString();//被保险人手机号
                String xubao_fill_address_str = xubao_fill_address_et.getText().toString();//被保险人地址
                String xubao_fill_brand_model_str = xubao_fill_brand_model_et.getText().toString();//厂牌型号
                String xubao_fill_plate_num_str = xubao_fill_plate_num_et.getText().toString();//号牌号码
                String xubao_fill_vin_str = xubao_fill_vin_et.getText().toString();//就是车架号
                String xubao_fill_engine_str = xubao_fill_engine_et.getText().toString();//发动机编号
                String xubao_fill_useNature_str = xubao_fill_useNature_et.getText().toString();//车辆使用性质
                String xubao_fill_carType_str = xubao_fill_carType_et.getText().toString();//车辆类型
                String xubao_fill_regtime_str = xubao_fill_regtime_tv.getText().toString();//车辆注册时间
                String xubao_fill_passengers_str = xubao_fill_passengers_et.getText().toString();//车辆核定载客人数
                String xubao_fill_weight_str = xubao_fill_weight_et.getText().toString();//车辆核定载质量
                String xubao_fill_driving_area_str = xubao_fill_driving_area_et.getText().toString();//车辆行驶区域
                String xubao_fill_place_of_production_str = xubao_fill_place_of_production_tv.getText().toString();//车辆产地

                if (TextUtils.isEmpty(xubao_fill_applicant_str)) {

                    MyToast.showToast("请填写投保人姓名", context);
                    return;
                }
//                if (picMap.get(1) == null) {
//
//                    MyToast.showToast("请上传身份证", context);
//                    return;
//                }
                if (TextUtils.isEmpty(xubao_name_str)) {

                    MyToast.showToast("请填写被保险人姓名", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_id_number_str)) {

                    MyToast.showToast("请填写被保险人身份证号", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_tel_str)) {

                    MyToast.showToast("请填写被保险人手机号", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_address_str)) {

                    MyToast.showToast("请填写被保险人地址", context);
                    return;
                }
//                //车辆信息
//                if (picMap.get(2) == null) {
//
//                    MyToast.showToast("请上传行驶证", context);
//                    return;
//                }
                if (TextUtils.isEmpty(xubao_fill_brand_model_str)) {

                    MyToast.showToast("请填写厂牌型号", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_plate_num_str)) {

                    MyToast.showToast("请填写号牌号码", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_vin_str)) {

                    MyToast.showToast("请填写VIN", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_engine_str)) {

                    MyToast.showToast("请填写发动机号", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_useNature_str)) {

                    MyToast.showToast("请填写使用性质", context);
                    return;
                }

                if (natureType == 3) {

                    MyToast.showToast("请填写使用性质, 营运或非营运", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_carType_str)) {

                    MyToast.showToast("请填写车辆种类", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_regtime_str)) {

                    MyToast.showToast("请填写注册日期", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_passengers_str)) {

                    MyToast.showToast("请填写核定载客数", context);
                    return;
                }

                if (natureType == 1 && TextUtils.isEmpty(xubao_fill_weight_str)) {

                    MyToast.showToast("请填写核定载质量", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_driving_area_str)) {

                    MyToast.showToast("请填写行驶区域", context);
                    return;
                }

                if (TextUtils.isEmpty(xubao_fill_place_of_production_str)) {

                    MyToast.showToast("请选择车辆产地", context);
                    return;
                }

//                if (picMap.get(3) == null) {
//
//                    MyToast.showToast("请上传人车照片", context);
//                    return;
//                }


                try {
                    JSONObject jsonObject = new JSONObject();//保险人信息

                    jsonObject.put("name", xubao_name_str);
                    jsonObject.put("id_number", xubao_id_number_str);
                    jsonObject.put("tel", xubao_fill_tel_str);
                    jsonObject.put("address", xubao_fill_address_str);

                    JSONObject jsonObject1 = new JSONObject();//被保险车辆信息

                    jsonObject1.put("brand_model", xubao_fill_brand_model_str);
                    jsonObject1.put("plate_num", xubao_fill_plate_num_str);
                    jsonObject1.put("vin_code", xubao_fill_vin_str);
                    jsonObject1.put("engine_num", xubao_fill_engine_str);
                    jsonObject1.put("use_nature", natureType + "");
                    jsonObject1.put("vehicle_type", xubao_fill_carType_str);
                    jsonObject1.put("regtime", xubao_fill_regtime_str);
                    jsonObject1.put("passengers", xubao_fill_passengers_str);
                    jsonObject1.put("weight", xubao_fill_weight_str);
                    jsonObject1.put("driving_area", xubao_fill_driving_area_str);
                    jsonObject1.put("vehicle_origin", xubao_fill_place_of_production_str);


                    bundle.putString("vehicle_info", jsonObject1.toString());
                    bundle.putString("insurant_info", jsonObject.toString());

                    UtilLog.e("---vehicle_info----", jsonObject1.toString());
                    UtilLog.e("---insurant_info----", jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                //三个图片
                bundle.putString("id_url", picMap.get(1));
                bundle.putString("drive_url", picMap.get(2));
                bundle.putString("people_car_url", picMap.get(3));

                //这三个重复
                bundle.putString("applicant", xubao_fill_applicant_str);
                bundle.putString("name", xubao_name_str);
                bundle.putString("brand_model", xubao_fill_brand_model_str);


                startActivity(new Intent(this, BusinessInsuranceChooseLevelActivity.class).putExtras(bundle));


                break;

        }

    }

    private void showPop(View view) {

        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout, null);

        ListView popList = ((ListView) contentView.findViewById(R.id.item_popList));

        ArrayAdapter adapter = new ArrayAdapter<String>(context, R.layout.support_simple_spinner_dropdown_item, produceLists);

        popList.setAdapter(adapter);

        final PopupWindow popupWindow = new PopupWindow(contentView, view.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_white_bg));
        popupWindow.showAsDropDown(view);

        popList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                xubao_fill_place_of_production_tv.setText(produceLists.get(position));

                popupWindow.dismiss();
            }
        });

    }

    public String bitmaptoString(Bitmap bitmap) {
        // 将Bitmap转换成字符串

        String string = null;

        ByteArrayOutputStream bStream = new ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bStream);

        byte[] bytes = bStream.toByteArray();
        Base64Encoder base = new Base64Encoder();
        string = base.encode(bytes); //

        //  string = Base64.encodeToString(bytes, Base64.DEFAULT);

        return string;

    }

//    @Override
//    protected void resultPhotoPath(ImageView imageView, String s) {
//
//        String str = bitmaptoString(PictureUtil.getSmallBitmap(s));
//
//        String uid = BaseApplication.BasePreferences.readUserId();
//
//        String time = System.currentTimeMillis() + "";
//
//        String lock = UtilMD5.MD5Encode(UtilMD5.MD5Encode(uid + time, "UTF-8") + "zhang", "UTF-8");
//
//        switch (i) {
//
//            case 1:
//
//                postIDCard.userid = uid;
//                postIDCard.time = time;
//                postIDCard.lock = lock;
//                postIDCard.base = str;
//
//                postIDCard.execute(this);
//
//                break;
//
//            case 2:
//
//                postVehicleCard.userid = uid;
//                postVehicleCard.time = time;
//                postVehicleCard.lock = lock;
//                postVehicleCard.base = str;
//
//                postVehicleCard.execute(this);
//
//                break;
//
//            case 3:
//
//                xubao_fill_show_peopleCar_iv.setVisibility(View.VISIBLE);
//                xubao_fill_show_peopleCar_iv.setImageBitmap(PictureUtil.getSmallBitmap(s));
//
//                xubao_fill_summit_bt.setText("已选择");
//
//                break;
//
//        }
//
//        picMap.put(i, s);
//
//    }


}
